package com.cnswhy.cloud.server.app.controller.shop;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.core.constant.RedisConstant;
import com.cnswhy.cloud.server.app.controller.BaseController;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerBank;
import com.cnswhy.cloud.server.core.entity.ShopComment;
import com.cnswhy.cloud.server.core.entity.ShopOrder;
import com.cnswhy.cloud.server.core.query.QueryGenerator;
import com.cnswhy.cloud.server.core.service.shop.ShopCommentService;
import com.cnswhy.cloud.server.core.service.shop.ShopOrderService;
import com.cnswhy.cloud.server.core.vo.RespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述: 名品名店评论
 * 版权: Copyright (c) 2019
 * 公司: 小鹿科技
 * 作者: 张润泽
 * 版本: 1.0
 * 创建日期: 2019年8月13日
 * 创建时间: 下午7:30:46
 */
@Api(tags = "商品评论")
@Controller
@RequestMapping("/shop/comment")
@Slf4j
public class ShopCommentController extends BaseController {


    @Autowired
    private ShopCommentService shopCommentService;

    @Autowired
    private ShopOrderService shopOrderService;

    @Autowired
    private RedisService redisService;

    /**
     * 描述：评论
     * 作者：张润泽
     * 时间：2019年8月6日 下午6:50:20
     *
     * @param commentJson
     * @return
     */
    @ApiOperation(value = "发布评论", httpMethod = "POST")
    @ApiParam(name = "commentJson", value = "评论json字符串")
    @RequestMapping("/comment")
    @ResponseBody
    public RespVo comment(String commentJson, HttpServletRequest request) {
        RespVo respVo = new RespVo();
        try {
            if (StringUtils.isBlank(commentJson)) {
                respVo.setCode(RespVo.VALIDATE_ERROR);
                respVo.setMessage("参数异常");
                return respVo;
            }

            Customer loginCustomer = getLoginCustomer( request);
            ShopComment bo = JSON.parseObject(commentJson, ShopComment.class);

            // 查询订单
            ShopOrder order = shopOrderService.queryById(bo.getOrderId());
            if (order == null) {
                respVo.setCode(RespVo.CODE_ERROR);
                respVo.setMessage("订单不存在");
                return respVo;
            }

            // 保存商品评论
            //TODO
            /*List<ShopComment> list = bo.getCommentList();
            for (ShopComment comment : list)
            {
                comment.setIsAnonymous(bo.getIsAnonymous());
                comment.setCustomerId(loginCustomer.getId());
                comment.setMerchantId(order.getMerchantId());
                comment.setOrderId(bo.getOrderId());
                shopCommentService.save(comment);
            }*/

            // 保存商家评分
//            ShopMerchantScore merchantScore = bo.getMerchantScore();
//            merchantScore.setCustomerId(loginCustomer.getId());
//            merchantScore.setMerchantId(order.getMerchantId());
//            merchantScore.setOrderId(order.getId());
//            shopCommentService.saveMerchantScore(merchantScore);
//            
//            // 修改订单状态
//            ShopOrder orderEntity = new ShopOrder();
//            orderEntity.setId(order.getId());
//            orderEntity.setStatus(ShopOrderStatus._5.getValue());
//            orderEntity.setFinishTime(new Date());
//            shopOrderService.save(orderEntity);

            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("评论成功");
        } catch (Exception e) {
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("评论失败");
            log.error("保存名品名店订单评论异常：", e);
        }
        return respVo;
    }

    /**
     * 描述：查询产品评论列表
     * 作者：张润泽
     * 时间：2019年8月13日 下午8:49:08
     *
     * @param bo
     * @param request
     * @return
     */
    @ApiOperation(value = "查询商品评论", httpMethod = "POST")
    @ApiParam(name = "bo", value = "商品评论查询bO")
    @RequestMapping("/ajaxPage")
    @ResponseBody
    public RespVo ajaxPage(ShopComment bo,
                           @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest request) {
        RespVo respVo = new RespVo();
        try {
            if (bo.getProductId() == null) {
                respVo.setCode(RespVo.VALIDATE_ERROR);
                respVo.setMessage("参数异常");
                return respVo;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            QueryWrapper<ShopComment> queryWrapper = QueryGenerator.initQueryWrapper(bo);

            map.put("commentCount", shopCommentService.list(queryWrapper).size());
            map.put("urlPrefix", redisService.hget(RedisConstant.SYS_CONFIG_1, RedisConstant.FTP_PATH_PREFIX));
            queryWrapper.orderByDesc("create_time");
            Page<CustomerBank> page = new Page<>(currentPage, pageSize);
            map.put("pageList", page);
            respVo.setResult(map);
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("查询成功");
        } catch (Exception e) {
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("查询失败");
            log.error("查询名品名店评论列表异常：", e);
        }
        return respVo;
    }

    /**
     * 描述：查询评论详情
     * 作者：张润泽
     * 时间：2019年8月13日 下午8:49:08
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "评论详情", httpMethod = "POST")
    @ApiParam(name = "id", value = "评论ID", required = true)
    @RequestMapping("/detail")
    @ResponseBody
    public RespVo detail(Long id, HttpServletRequest request) {
        RespVo respVo = new RespVo();
        try {
            if (id == null) {
                respVo.setCode(RespVo.VALIDATE_ERROR);
                respVo.setMessage("参数异常");
                return respVo;
            }

            respVo.setResult(shopCommentService.getById(id));
            respVo.setCode(RespVo.CODE_SUCCESS);
            respVo.setMessage("查询成功");
        } catch (Exception e) {
            respVo.setCode(RespVo.CODE_ERROR);
            respVo.setMessage("查询失败");
            log.error("查询名品名店评论详情异常：", e);
        }
        return respVo;
    }
}
